package com.goods.business.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.ProductMapper;
import com.goods.business.mapper.ProductStockMapper;
import com.goods.business.service.ProductService;
import com.goods.common.model.business.Product;
import com.goods.common.model.business.ProductStock;
import com.goods.common.vo.business.ProductStockVO;
import com.goods.common.vo.business.ProductVO;
import com.goods.common.vo.system.PageVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductStockMapper productStockMapper;

    //物资资料分页列表展示
    @Override
    public PageVO<ProductVO> findProductList(Integer pageNum, Integer pageSize, String name, Long categoryId, String supplier, Integer status, String categorys) {
        PageHelper.startPage(pageNum,pageSize);
        Example example = new Example(Product.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status",status);
        if (!StringUtils.isEmpty(categorys)){
            String[] split = categorys.split(",");
            /*if (split.length >= 1){
                Long oneCategoryId = Long.parseLong(split[0]);
                criteria.andEqualTo("oneCategoryId",oneCategoryId);
            }
            if (split.length >= 2){
                Long twoCategoryId = Long.parseLong(split[1]);
                criteria.andEqualTo("twoCategoryId",twoCategoryId);
            }
            if (split.length >= 3){
                Long threeCategoryId = Long.parseLong(split[2]);
                criteria.andEqualTo("threeCategoryId",threeCategoryId);
            }*/
            switch (split.length){
                case 3 :
                    Long threeCategoryId = Long.parseLong(split[2]);
                    criteria.andEqualTo("threeCategoryId",threeCategoryId);
                case 2 :
                    Long twoCategoryId = Long.parseLong(split[1]);
                    criteria.andEqualTo("twoCategoryId",twoCategoryId);
                case 1 :
                    Long oneCategoryId = Long.parseLong(split[0]);
                    criteria.andEqualTo("oneCategoryId",oneCategoryId);
                    break;
            }
        }
        if (!StringUtils.isEmpty(name)){
            criteria.andLike("name","%" + name + "%");
        }
        List<Product> productList = productMapper.selectByExample(example);
        List<ProductVO> productVOList = productList.stream().map(product -> {
            ProductVO productVO = new ProductVO();
            BeanUtils.copyProperties(product, productVO);
            Long[] arr = new Long[]{productVO.getOneCategoryId(), productVO.getTwoCategoryId(), productVO.getThreeCategoryId()};
            productVO.setCategoryKeys(arr);
            return productVO;
        }).collect(Collectors.toList());
        PageInfo<Product> pageInfo = new PageInfo<>(productList);
        return new PageVO<>(pageInfo.getTotal(),productVOList);
    }

    //添加物资
    @Override
    public void addProduct(ProductVO productVO) {
        Product product = new Product();
        BeanUtils.copyProperties(productVO,product);
        String uuid = UUID.randomUUID().toString();
        product.setPNum(uuid);
        Calendar calendar = Calendar.getInstance();
        product.setCreateTime(calendar.getTime());
        product.setModifiedTime(calendar.getTime());
        //状态为未审核
        product.setStatus(2);
        Long[] categoryKeys = productVO.getCategoryKeys();
        product.setOneCategoryId(categoryKeys[0]);
        product.setTwoCategoryId(categoryKeys[1]);
        product.setThreeCategoryId(categoryKeys[2]);
        productMapper.insertSelective(product);
    }

    //编辑物资
    @Override
    public Product editProduct(Long id) {
        return productMapper.selectByPrimaryKey(id);
    }

    //更新物资
    @Override
    public void updateProduct(Product product) {
        product.setModifiedTime(new Date());
        productMapper.updateByPrimaryKeySelective(product);
    }

    //物资移入回收站
    @Override
    public void removeProduct(Long id) {
        Product product = new Product();
        product.setId(id);
        product.setModifiedTime(new Date());
        product.setStatus(1);
        productMapper.updateByPrimaryKeySelective(product);
    }

    //物资恢复到正常状态
    @Override
    public void backProduct(Long id) {
        Product product = new Product();
        product.setId(id);
        product.setModifiedTime(new Date());
        product.setStatus(0);
        productMapper.updateByPrimaryKeySelective(product);
    }

    //物资删除
    @Override
    public void deleteProduct(Long id) {
        productMapper.deleteByPrimaryKey(id);
    }

    //物资审核
    @Override
    public void publishProduct(Long id) {
        Product product = new Product();
        product.setId(id);
        product.setModifiedTime(new Date());
        product.setStatus(0);
        productMapper.updateByPrimaryKeySelective(product);
    }

    //查询物资库存
    @Override
    public PageVO<ProductStockVO> findProductStocks(Integer pageNum, Integer pageSize, String categorys, String name) {
        PageHelper.startPage(pageNum,pageSize);
        Example example = new Example(Product.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status",0);
        if (!StringUtils.isEmpty(categorys)){
            String[] split = categorys.split(",");
            /*if (split.length >= 1){
                Long oneCategoryId = Long.parseLong(split[0]);
                criteria.andEqualTo("oneCategoryId",oneCategoryId);
            }
            if (split.length >= 2){
                Long twoCategoryId = Long.parseLong(split[1]);
                criteria.andEqualTo("twoCategoryId",twoCategoryId);
            }
            if (split.length >= 3){
                Long threeCategoryId = Long.parseLong(split[2]);
                criteria.andEqualTo("threeCategoryId",threeCategoryId);
            }*/
            switch (split.length){
                case 3 :
                    Long threeCategoryId = Long.parseLong(split[2]);
                    criteria.andEqualTo("threeCategoryId",threeCategoryId);
                case 2 :
                    Long twoCategoryId = Long.parseLong(split[1]);
                    criteria.andEqualTo("twoCategoryId",twoCategoryId);
                case 1 :
                    Long oneCategoryId = Long.parseLong(split[0]);
                    criteria.andEqualTo("oneCategoryId",oneCategoryId);
                    break;
            }
        }
        if (!StringUtils.isEmpty(name)){
            criteria.andLike("name","%" + name + "%");
        }
        List<Product> productList = productMapper.selectByExample(example);
        List<ProductStockVO> productStockVOList = productList.stream().map(product -> {
            ProductStockVO productStockVO = new ProductStockVO();
            productStockVO.setId(product.getId());
            productStockVO.setName(product.getName());
            productStockVO.setPNum(product.getPNum());
            productStockVO.setModel(product.getModel());
            productStockVO.setUnit(product.getUnit());
            productStockVO.setRemark(product.getRemark());
            productStockVO.setImageUrl(product.getImageUrl());
            ProductStock productStock = new ProductStock();
            productStock.setPNum(product.getPNum());
            ProductStock stock = productStockMapper.selectOne(productStock);
            if (null != stock){
                productStockVO.setStock(stock.getStock());
            }
            return productStockVO;
        }).collect(Collectors.toList());
        PageInfo<Product> pageInfo = new PageInfo<>(productList);
        return new PageVO<>(pageInfo.getTotal(),productStockVOList);
    }

}
